package com.devnn.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.devnn.base.R;
import com.devnn.entity.VersionInfo;
import com.devnn.view.DownLoadDialog;
import com.devnn.view.ConfirmDialog;

/**
 * Created by devnn on 16/1/14.
 */
public class BaseUtil {
    private static Toast TOAST = null;
    private static String TAG = "BaseUtil";

    public static int getVerCode(Context context) {
        int verCode = -1;
        try {
            verCode = context.getPackageManager().getPackageInfo(context.getPackageName()
                    , 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }
        return verCode;
    }

    public static String getVerName(Context context) {
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(context.getPackageName()
                    , 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, e.getMessage());
        }
        return verName;
    }

    public static void checkVersion(final VersionInfo newVersion, final Context context, boolean show) {
        int verCode = getVerCode(context);
        String verName = getVerName(context);
        if (verCode == newVersion.getAndVersionCode()) {
            if (show) {
                ConfirmDialog dialog = new ConfirmDialog(context);
                dialog.show();
                dialog.setMessage("当前版本:v" + verName + ",已经是最新版本");
                dialog.setCancelVisiable(View.GONE);
            }
            return;
        } else if (verCode < newVersion.getAndVersionCode()) {
            ConfirmDialog dialog = new ConfirmDialog(context);
            dialog.show();
            dialog.setMessage("当前版本:v" + verName + ",最新版本是:v" + newVersion.getAndVersionName() + ",是否更新?");
            dialog.setOnSelectedListener(new ConfirmDialog.OnSelectedListener() {

                @Override
                public void onSureSelected() {
                    // TODO Auto-generated method stub
                    DownLoadDialog dialog = new DownLoadDialog(context);
                    dialog.show();
                    dialog.startDownLoad(newVersion.getAndDownUrl());
                }

                @Override
                public void onCancelSelected() {
                    // TODO Auto-generated method stub

                }
            });
        } else {
            ConfirmDialog dialog = new ConfirmDialog(context);
            dialog.show();
            dialog.setMessage("当前版本:v" + verName + ",服务器版本是:v" + newVersion.getAndVersionName() + ",您的版本已超前");
            dialog.setOnSelectedListener(new ConfirmDialog.OnSelectedListener() {

                @Override
                public void onSureSelected() {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onCancelSelected() {
                    // TODO Auto-generated method stub

                }
            });
        }
    }

    public static void playVoice(Context context, int res) {
        SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);

        final int sourceid = soundPool.load(context, res, 0);
        //播放音频，第二个参数为左声道音量;第三个参数为右声道音量;第四个参数为优先级；第五个参数为循环次数，0不循环，-1循环;第六个参数为速率，速率最低0.5最高为2，1代表正常速度
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                // TODO Auto-generated method stub
                soundPool.play(sourceid, 1, 1, 0, 0, 1);

            }
        });
//        soundPool.set

    }
    //进入gps设置界面
    public static void openGPS(Context context) {
        Intent intent =  new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    public static boolean isLocationServiceOpen(Context context){
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }
    public static void showTextToast(Context context,String msg) {
        if (TOAST == null) {
            TOAST = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        } else {
            TOAST.setText(msg);
        }
        TOAST.show();
    }
    public static String getMetaData(Context context,String name) {
        try {
            String channel = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA).metaData.getString(name);
            return channel;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
